﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Shop_ShopSaleDetail_List : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        ReadAction();

        if (!IsPostBack)
        {
            BindData();              //梆定所有数据
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.g.CurrentPageIndex = 0;
        this.gPager.PageIndex = 0;

        BindData();
    }

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Columns.Remove("Price_Lastin");
            dt.Columns.Remove("LastinSum");
            dt.Columns.Remove("GoodID");
            dt.Columns[0].ColumnName = "序号";
            dt.Columns[1].ColumnName = "店铺序号";
            dt.Columns[2].ColumnName = "店铺编号";
            dt.Columns[3].ColumnName = "店铺名称";
            dt.Columns[4].ColumnName = "产品编号";
            dt.Columns[5].ColumnName = "产品名称";
            dt.Columns[6].ColumnName = "原价";
            dt.Columns[7].ColumnName = "Vip价";
            dt.Columns[8].ColumnName = "BV值";
            dt.Columns[9].ColumnName = "销售数量";
            dt.Columns[10].ColumnName = "VIP总额";
            dt.Columns[11].ColumnName = "BV总值";
            dt.Columns[12].ColumnName = "库存数量";

            Utility.DataTableToExcel.DataTableToExcelAndDownload(dt, "ShopSaleDetail.xls");
        }
        else
        {
            PublicFunction.GoError(ErrorNumber.NoData, "没有数据，不能导出到Excel!", "ShopSaleDetail");
        }
    }

    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("Shop_List.aspx");
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void g_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            e.Item.SetRenderMethodDelegate(new RenderMethod(DrawGridFooterForG));
        }
    }

    protected void DrawGridFooterForG(HtmlTextWriter output, Control ctl)
    {
        int Amount = 0;         //总销售数量
        int StockAmount = 0;    //库存数量
        double VipSum = 0;      //销售总额
        double LastinSum = 0;   //进货总价
        double BVSum = 0;       //BV总积分

        try
        {
            if (ViewState["Price_Lastian"].ToString() == "Yes")
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        Amount += Shove._Convert.StrToInt(dt.Rows[i]["Amount"].ToString(), 0);
                        StockAmount += Shove._Convert.StrToInt(dt.Rows[i]["StockAmount"].ToString(), 0);
                        VipSum += Shove._Convert.StrToDouble(dt.Rows[i]["VipSum"].ToString(), 0);
                        LastinSum += Shove._Convert.StrToDouble(dt.Rows[i]["LastinSum"].ToString(), 0);
                        BVSum += Shove._Convert.StrToDouble(dt.Rows[i]["BVSum"].ToString(), 0);
                    }

                    output.Write("<td align='center'>总计</td>");
                    output.Write("<td align='center'></td>");
                    output.Write("<td align='center'></td>");
                    output.Write("<td align='center'></td>");
                    output.Write("<td align='center'></td>");
                    output.Write("<td align='center'></td>");
                    output.Write("<td align='center'></td>");
                    output.Write("<td align='center'>" + Amount + "</td>");
                    output.Write("<td align='center'>" + StockAmount + "</td>");
                    output.Write("<td align='center'>" + VipSum + "</td>");
                    output.Write("<td align='center'>" + LastinSum + "</td>");
                    output.Write("<td align='center'>" + BVSum + "</td>");
                    output.Write("<td align='center'></td>");
                }
            }
            else
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        Amount += Shove._Convert.StrToInt(dt.Rows[i]["Amount"].ToString(), 0);
                        StockAmount += Shove._Convert.StrToInt(dt.Rows[i]["StockAmount"].ToString(), 0);
                        VipSum += Shove._Convert.StrToDouble(dt.Rows[i]["VipSum"].ToString(), 0);
                        BVSum += Shove._Convert.StrToDouble(dt.Rows[i]["BVSum"].ToString(), 0);
                    }

                    output.Write("<td align='center'>总计</td>");
                    output.Write("<td align='center'></td>");
                    output.Write("<td align='center'></td>");
                    output.Write("<td align='center'></td>");
                    output.Write("<td align='center'></td>");
                    output.Write("<td align='center'></td>");
                    output.Write("<td align='center'>" + Amount + "</td>");
                    output.Write("<td align='center'>" + StockAmount + "</td>");
                    output.Write("<td align='center'>" + VipSum + "</td>");
                    output.Write("<td align='center'>" + BVSum + "</td>");
                    output.Write("<td align='center'></td>");
                }
            }
        }
        catch 
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Amount += Shove._Convert.StrToInt(dt.Rows[i]["Amount"].ToString(), 0);
                    StockAmount += Shove._Convert.StrToInt(dt.Rows[i]["StockAmount"].ToString(), 0);
                    VipSum += Shove._Convert.StrToDouble(dt.Rows[i]["VipSum"].ToString(), 0);
                    BVSum += Shove._Convert.StrToDouble(dt.Rows[i]["BVSum"].ToString(), 0);
                }

                output.Write("<td align='center'>总计</td>");
                output.Write("<td align='center'></td>");
                output.Write("<td align='center'></td>");
                output.Write("<td align='center'></td>");
                output.Write("<td align='center'></td>");
                output.Write("<td align='center'></td>");
                output.Write("<td align='center'>" + Amount + "</td>");
                output.Write("<td align='center'>" + StockAmount + "</td>");
                output.Write("<td align='center'>" + VipSum + "</td>");
                output.Write("<td align='center'>" + BVSum + "</td>");
                output.Write("<td align='center'></td>");
            }
        }
    }

    protected void g_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            DataRow dr = drv.Row;

            string GoodID = dr["GoodID"].ToString();
            string ShopID = dr["ShopID"].ToString();

            string StartDay = "1900-12-12";
            string EndDay = "1900-12-12";

            if ((wdcStart.Value != null) && (wdcEnd.Value) != null)
            {
                DateTime dtStart = Shove._Convert.StrToDateTime("1900-12-12", "1900-12-12");        //销售统计开始时间
                DateTime dtEnd = Shove._Convert.StrToDateTime("1900-12-12", "1900-12-12");

                try
                {
                    dtStart = Shove._Convert.StrToDateTime(wdcStart.Value.ToString(), "1900-12-12");
                    dtEnd = Shove._Convert.StrToDateTime(wdcEnd.Value.ToString(), "1900-12-12");
                }
                catch { }

                if (dtStart > dtEnd)
                {
                    Shove._Web.JavaScript.Alert(this.Page, "销售统计的开始时间不能大于结束时间!");
                }
                else
                {
                    StartDay = dtStart.ToString("yyyy-MM-dd");
                    EndDay = dtEnd.ToString("yyyy-MM-dd");
                }
            }

            HyperLink Detail = (HyperLink)e.Item.Cells[9].FindControl("hlDetail");
            Detail.NavigateUrl = "ShopSaleDetail_List2.aspx?GoodID=" + GoodID + "&ShopID="+ ShopID + "&StartDay=" + StartDay + "&EndDay=" + EndDay;
        }
    }

    /// <summary>
    /// 获取上个页面传过来的值(string ShopID,string Day)
    /// </summary>
    private void ReadAction()
    {
        hShopID.Value = Shove._Web.Utility.GetRequest("ShopID");
    }

    /// <summary>
    /// 梆定条件查询出来的数据
    /// </summary>
    private void BindData()
    {
        DataSet ds = null;

        long ShopID = Shove._Convert.StrToLong(this.hShopID.Value, 0);          
        string SalesStart = "1900-12-12";
        string SalesEnd = "1900-12-12";
        string GoodName = "";
        string GoodNo = "";

        if ((wdcStart.Value != null) && (wdcEnd.Value) != null)
        {
            DateTime dtStart = Shove._Convert.StrToDateTime("1900-12-12", "1900-12-12");        //销售统计开始时间
            DateTime dtEnd = Shove._Convert.StrToDateTime("1900-12-12", "1900-12-12");

            try
            {
                dtStart = Shove._Convert.StrToDateTime(wdcStart.Value.ToString(), "1900-12-12");
                dtEnd = Shove._Convert.StrToDateTime(wdcEnd.Value.ToString(), "1900-12-12");
            }
            catch { }

            if (dtStart > dtEnd)
            {
                Shove._Web.JavaScript.Alert(this.Page, "销售统计的开始时间不能大于结束时间!");
            }
            else
            {
                SalesStart = dtStart.ToString("yyyy-MM-dd");
                SalesEnd = dtEnd.ToString("yyyy-MM-dd");
            }
        }

        //更新表列头文字
        try
        {
            DataTable dt = new DAL.Tables.tbl_Shop().Open("", "ID=" + Shove._Convert.StrToLong(hShopID.Value,0),"");

            if (dt != null && dt.Rows.Count > 0)
            {
                if (SalesStart == "1900-12-12" || SalesEnd == "1900-12-12")
                {
                    this.WebPanel1.Header.Text = dt.Rows[0]["ShopName"] + " --产品销售明细 (" + DateTime.Now.ToShortDateString() + ")";
                }
                else
                {
                    this.WebPanel1.Header.Text = dt.Rows[0]["ShopName"] + " --产品销售明细 (" + SalesStart + " - " + SalesEnd + ")";
                }
            }
        }
        catch { }

        if (this.ddlType.SelectedValue == "GoodNo")
        {
            GoodNo = this.txtCondition.Text.Trim();
        }
        else if (this.ddlType.SelectedValue == "GoodName")
        {
            GoodName = this.txtCondition.Text.Trim();
        }

        try
        {
            DAL.Procedures.P_Site_SalesGoodDetail(ref ds, SalesStart, SalesEnd, ShopID, GoodName,GoodNo);

            dt = ds.Tables[0];
        }
        catch { }

        this.g.DataSource = dt;
        this.g.DataBind();
    }

}


